﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CE.iPhone.PList;
using System.IO;
using System.Xml;
using CE.iPhone.PList.Internal;
using System.IO.Compression;

namespace CE.iPhone.PList.Test {
    class Program {
        static void Main(string[] args) {
            
            Console.WriteLine(GetIphonePlistInfo("CFBundleIdentifier", @"C:\Users\Administrator\Desktop\bk\500.ipa") );
            Console.WriteLine(GetIphonePlistInfo("CFBundleName", @"C:\Users\Administrator\Desktop\bk\500.ipa"));
            Console.ReadKey();
        }

        public static  void stream2file(byte[] bytes, string fileName)
        {                        
            FileStream fs = new FileStream(fileName, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(bytes);
            bw.Close();
            fs.Close();
        }

        public static string GetIphonePlistInfo(string attr, string ipaAddress)
        {
            string values = string.Empty;
            string infoPlistAddress = UnzipHelper.unzipFileIOS2(ipaAddress);
            PListRoot root = PListRoot.Load(infoPlistAddress);
            using (MemoryStream memStream = new MemoryStream())
            {
                root.Save(memStream, PListFormat.Xml);
                byte[] bys = memStream.ToArray();
                PListDict dic = (PListDict)root.Root;
                PListString name = (PListString)dic[attr];
                UTF8Encoding utf8 = new UTF8Encoding();
                Byte[] encodedBytes = utf8.GetBytes(name.Value);
                string  decodedString = utf8.GetString(encodedBytes);
                values = decodedString;
            }
            return values;

        }

         

    }
}
